﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using TSNPro.Utils;

namespace TSNPro.Base
{
    public class CommonConfig
    {
        // Properties
        public string DeviceType { get; private set; }
        private string configFilePath;
        private AppSettingsReader setting;

        // Private constructor to ensure Singleton pattern
        private CommonConfig()
        {
            try
            {
                // Load main config to retrieve the device type
                JObject mainObj = JsonUtils.LoadMainConfig();
                DeviceType = mainObj["DeviceType"].ToString();

                // Build config file path
                configFilePath = Path.Combine("./Config", DeviceType, "DeviceConfig.ini");

                // Initialize the setting using AppSettingsReader for configuration files
                setting = new AppSettingsReader();

                // Set the encoding to UTF-8 (handled at the file level in C#)
                // .NET does not directly support IniFormat, consider using a custom INI parser if needed
                // .NET can read app settings with ConfigurationManager, but INI handling is different

                // Example usage for reading specific setting, replace with actual config key
                var someSetting = setting.GetValue("SomeKey", typeof(string));
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error initializing DeviceVar: {ex.Message}");
            }
        }

        static CommonConfig _instance = new CommonConfig();
        // Singleton instance
        public static CommonConfig Instance()
        {
            return _instance;
        }
    }
}
